/*
 * AddCategory.java
 *
 * Created on __DATE__, __TIME__
 */

package gui;

import java.awt.Frame;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import core.Category;
import core.Core;
import core.UserEntry;

/**
 *
 * @author  __USER__
 */
public class AddCategory extends javax.swing.JDialog {

	/** Creates new form AddCategory */
	private Frame parent = null;;
	private Core core = null;
	private List<Category> categories = null;
	private EntryWindow father = null;

	void setFather(EntryWindow father_){
		father = father_;
	}
	
	public AddCategory(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		setUndecorated(false);
		core = Core.get();
		this.parent = parent;
		initComponents();
		initTable();
	}

	public void initTable() {
		categories = core.getCategories();
		Iterator<Category> i = categories.iterator();

		DefaultTableModel model = null;

		model = (DefaultTableModel) jTable2.getModel();
		model.setNumRows(0);
		model.setColumnCount(0);
		model.addColumn("Name");

		while (i.hasNext()) {
			Category current = (Category) i.next();
			model.addRow(new Object[] { current.getName() });
		}
		father.setComboBox();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTable2 = new javax.swing.JTable();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Modify Categories");

		jPanel1.setBackground(new java.awt.Color(141, 178, 227));

		jTable2.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null } }, new String[] { "Title 1",
						"Title 2", "Title 3", "Title 4" }));
		jScrollPane2.setViewportView(jTable2);

		jButton1.setText("Create");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callCreateCategory(evt);
			}
		});

		jButton2.setText("Delete");
		jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callDeleteCategory(evt);
			}
		});

		jButton3.setText("Cancel");
		jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callCancel(evt);
			}
		});

		jButton4.setText("Close");
		jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callCloseWindow(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jScrollPane2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												165,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												67, Short.MAX_VALUE)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jButton4)
														.addComponent(jButton1)
														.addComponent(jButton2)
														.addComponent(jButton3))
										.addGap(35, 35, 35)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												jScrollPane2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												356,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(73, 73, 73))
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(31, 31, 31)
										.addComponent(jButton1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jButton2)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jButton3)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												272, Short.MAX_VALUE)
										.addComponent(jButton4)
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void callCloseWindow(java.awt.event.MouseEvent evt) {
		father.setComboBox();
		this.dispose();
	}

	private void callCancel(java.awt.event.MouseEvent evt) {
		father.setComboBox();
		this.dispose();
	}

	private void callDeleteCategory(java.awt.event.MouseEvent evt) {
		int line = jTable2.getSelectedRow();
		if (line == -1 || line >= categories.size()){
			DialogWindow dw = new DialogWindow(null, true, "Error",
					"No selected line", 1, true);
			dw.setVisible(true);
			return;
		}
		core.deleteCategory(categories.get(line).getId());
		initTable();
		father.setComboBox();
	}

	private void callCreateCategory(java.awt.event.MouseEvent evt) {
		EnterCategory enterCatetoryWindow = new EnterCategory(this.parent, true);
		enterCatetoryWindow.setFather(this);
		enterCatetoryWindow.setVisible(true);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				AddCategory dialog = new AddCategory(new javax.swing.JFrame(),
						true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTable jTable2;
	// End of variables declaration//GEN-END:variables

}